package com.atguigu.condition;

import com.atguigu.bean.RainBow;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {


    /***
     *
     * @param importingClassMetadata 当前类的注解信息
     * @param registry  BeanDefinition 注册类
     *      把所有需要添加到容器中的bean,
     *      registry.registerBeanDefinition()手动注册;
     */
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        //ioc容器中是否有红色
        boolean definition = registry.containsBeanDefinition("com.atguigu.bean.Red");
        boolean definition1 = registry.containsBeanDefinition("com.atguigu.bean.Blue");
        if (definition&&definition1){
            //指定Bean定义信息,(Bean的类型,Bean..)
            RootBeanDefinition beanDefinition = new RootBeanDefinition(RainBow.class);
            //注册一个Bean,指定bean名
            registry.registerBeanDefinition("rainBow",beanDefinition);
        }
    }
}
